package com.xblog.controller;


import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;
import com.xblog.model.Blog;
import com.xblog.service.FileService;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Created by leo on 2017-09-13.
 */
public class BlogController extends Controller {

    public void index() {
        List<Blog> blogs = Blog.blogDao.find("select *from blog");
        setAttr("blogs", blogs);
        render("/view/list.jsp");
    }

    /*查询代码*/
    public void result() {
        String sql = "";
        sql ="select *from blog where id like ";
        sql +="'%";
         // sql +="\"";
        sql +=getPara("id");
        sql += "%'";
        List<Blog> blogs = Blog.blogDao.find(sql);
        setAttr("blogs", blogs);
        System.out.println("查询到的数据为"+blogs.size());
        render("/view/result.jsp");
    }

    public void result2() {
        String sql = "";
        sql ="select *from blog where title like ";
        sql +="'%";
        // sql +="\"";
        sql +=getPara("title");
        sql += "%'";
        List<Blog> blogs = Blog.blogDao.find(sql);
        setAttr("blogs", blogs);
        System.out.println("查询到的数据为"+blogs.size());
        render("/view/result.jsp");
    }

    public void result3() {
        String sql = "";
        sql ="select *from blog where content like ";
        sql +="'%";
        // sql +="\"";
        sql +=getPara("content");
        sql += "%'";
        List<Blog> blogs = Blog.blogDao.find(sql);
        setAttr("blogs", blogs);
        System.out.println("查询到的数据为"+blogs.size());
        render("/view/result.jsp");
    }


    public void form() {
        Integer id = getParaToInt(0);
        if (id != null && id > 0) {
            setAttr("blog", Blog.blogDao.findById(id));


        }
        render("/view/from.jsp");

    }

    /*提交方法*/
    public void submit() {
        Blog blog = getModel(Blog.class, "blog");
        blog.save();
        index();
    }

    /*修改*/
    public void edit() {
        //因为修改的方法和新建都是在页面进行展示的，所以可以进行性跳转到这个页面用三元运算进行判断
        form();

    }

    /*删除*/
    public void del() {
        //Blog.blogDao.deleteById(getPara(0));
        Blog.blogDao.deleteById(getParaToInt());
        redirect("/blog");
    }

     /*更新*/
    public void update() {
        Blog blog = getModel(Blog.class, "blog");
        blog.update();
        redirect("/blog");
    }



    /*文件上传*/
    public void uploadFile(){
        UploadFile uploadFile=this.getFile();
        String fileName=uploadFile.getOriginalFileName();
        File file=uploadFile.getFile();
        FileService fs=new FileService();
       File t=new File("S:\\file\\"+ UUID.randomUUID().toString());
        //this.renderHtml("success,<a href=\"./\">back</a>");
      //  redirect("/blog");
        render("/view/uploadSuccess.jsp");
        /*try {
            t.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        fs.fileChannelCopy(file, t);
        file.delete();
        this.renderHtml("success,<a href=\"./\">back</a>");*/
    }


}
















